package com.logic.battle
{
	import com.action.ActionDie;
	import com.action.ActionStand;
	import com.action.BaseAction;
	import com.net.udp.UDPServer;
	import com.stage.item.Player;

	/**
	 * 
	 * 状态机
	 * 状态转换处理
	 * 
	 */
	public class StateMachine
	{
		/**
		 * 输入指令改变状态
		 * 先不考虑转身状态
		 * 强制转换
		 */
		private static const COMMAND_STATE:Array = [
		//是否已调	0		1		1		1		1		1		1		1		0
		//			NULL,	STAND,	MOVE,	ATTACK,	HURT,	JUMP,	DIE,	DEF,	TURN
		/*NULL*/	[0,		0,		0,		0,		0,		0,		0,		0,		0	],
		/*STAND*/	[0,		1,		1,		1,		0,		1,		0,		1,		0	],
		/*MOVE*/	[0,		1,		1,		1,		0,		1,		0,		1,		0	],
		/*ATTACK*/	[0,		1,		1,		0,		0,		0,		0,		0,		0	],
		/*HURT*/	[0,		0,		0,		0,		0,		0,		0,		0,		0	],
		/*JUMP*/	[0,		0,		1,		1,		0,		1,		0,		1,		0	],
		/*DIE*/		[0,		0,		0,		0,		0,		0,		0,		0,		0	],
		/*DEF*/		[0,		1,		1,		0,		0,		0,		0,		1,		0	],		
		/*TURN*/	[0,		0,		0,		0,		0,		0,		0,		0,		0	]
		];
		
		/**
		 * 中断方式的状态转换
		 * 先不考虑转身状态
		 * 强制转换
		 */
		private static const INTERRUPT_STATE:Array = [
			//是否已调	0		0		0		0		0		0		0		0		0
			//			NULL,	STAND,	MOVE,	ATTACK,	HURT,	JUMP,	DIE,	DEF,	TURN
			/*NULL*/	[0,		0,		0,		0,		0,		0,		0,		0,		0	],
			/*STAND*/	[0,		1,		1,		1,		1,		1,		0,		1,		0	],
			/*MOVE*/	[0,		1,		1,		1,		1,		1,		0,		1,		0	],
			/*ATTACK*/	[0,		1,		1,		0,		0,		0,		0,		1,		0	],
			/*HURT*/	[0,		0,		0,		0,		1,		0,		1,		0,		0	],
			/*JUMP*/	[0,		0,		1,		1,		1,		1,		0,		1,		0	],
			/*DIE*/		[0,		0,		0,		0,		0,		0,		0,		0,		0	],
			/*DEF*/		[0,		1,		1,		0,		1,		0,		0,		1,		0	],		
			/*TURN*/	[0,		0,		0,		0,		0,		0,		0,		0,		0	]
		];
		
		/**
		 * 自然状态转换
		 * 在上一状态结束后自然转化到另一状态
		 * 非强制转换
		 */
		private static const AUTO_STATE:Array = [
			//是否已调	0		0		0		0		0		0		0		0		0
			//			NULL,	STAND,	MOVE,	ATTACK,	HURT,	JUMP,	DIE,	DEF,	TURN
			/*NULL*/	[0,		0,		0,		0,		0,		0,		0,		0,		0	],
			/*STAND*/	[0,		1,		1,		0,		1,		1,		0,		1,		0	],
			/*MOVE*/	[0,		1,		1,		0,		1,		1,		0,		1,		0	],
			/*ATTACK*/	[0,		1,		1,		0,		0,		0,		0,		1,		0	],
			/*HURT*/	[0,		0,		0,		0,		1,		0,		1,		0,		0	],
			/*JUMP*/	[0,		0,		1,		0,		1,		1,		0,		1,		0	],
			/*DIE*/		[0,		0,		0,		0,		0,		0,		0,		0,		0	],
			/*DEF*/		[0,		1,		1,		0,		1,		0,		0,		1,		0	],		
			/*TURN*/	[0,		0,		0,		0,		0,		0,		0,		0,		0	]
		];
		
		/**
		 * 自然的状态转换，非持续性状态在结束时的自然自然转换
		 */
		public static function autoState(action:BaseAction):void{
			switch(action.type){
				case BaseAction.ATTACK:
					var player:Player = action.player;
					player.action = new ActionStand(player);
					player.action.start(player.dir);
//					var args:Object = {};
//					args.dir = player.dir;
//					args.toType = BaseAction.STAND;
//					UDPServer.getInstance().battleGroup.broadcast("onCommand", BattleLogic.thisPlayer.data.aid, args);
					break;
				case BaseAction.JUMP:
					var player:Player = action.player;
					player.action = new ActionStand(player);
					player.action.start(player.dir);
//					var args:Object = {};
//					args.dir = player.dir;
//					args.toType = BaseAction.STAND;
//					UDPServer.getInstance().battleGroup.broadcast("onCommand", BattleLogic.thisPlayer.data.aid, args);
					break;
				case BaseAction.HURT:
					var player:Player = action.player;
					if(player.property.hp > 0){
						player.action = new ActionStand(player);
						player.action.start(player.dir);
//						var args:Object = {};
//						args.dir = player.dir;
//						args.toType = BaseAction.STAND;
//						UDPServer.getInstance().battleGroup.broadcast("onCommand", BattleLogic.thisPlayer.data.aid, args);
					}else{
						player.action = new ActionDie(player);
						player.action.start(player.dir);
//						var args:Object = {};
//						args.dir = player.dir;
//						args.toType = BaseAction.DIE;
//						UDPServer.getInstance().battleGroup.broadcast("onCommand", BattleLogic.thisPlayer.data.aid, args);
					}
					break;
				case BaseAction.TURN:
					var player:Player = action.player;
					player.action = new ActionStand(player);
					player.action.start(player.dir);
//					var args:Object = {};
//					args.dir = player.dir;
//					args.toType = BaseAction.STAND;
//					UDPServer.getInstance().battleGroup.broadcast("onCommand", BattleLogic.thisPlayer.data.aid, args);
					break;
			}
		}

		
		/**
		 * 输入指令对状态进行转换
		 * 一般是客户端玩家输入指令达到指令转化
		 * 满足条件的才能转化
		 * 主机段处理
		 */
		public static function commandState(action:BaseAction, toType:int, args:Object = null):void{
			if(COMMAND_STATE[action.type][toType] == 1){
				UDPServer.getInstance().battleGroup.broadcast("onCommand", action.player.data.aid, args);
			}
		}
		
		
		/**
		 * 状态被中断时的转换，如接收到状态指令时进行的转换
		 * 一般是强制转换的
		 * 一般是接收到主机的状态转换指令
		 */
		public static function interruptState(action:BaseAction, toType:int, args:Object = null):void{
			
		}
	}
}